package com.orange.adminapi.controller.repairOrder;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.service.repairOrder.RepairOrderService;
import com.orange.core.pojo.BR;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.repairOrder.dto.RepairOrderDTO;
import com.orange.core.pojo.entity.repairOrder.vo.RepairOrderVO;
import com.orange.core.pojo.req.repairOrder.RepairOrderPageReq;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>ClassName:RepairOrderController</p>
 * <p>Description: 报修管理controller</p>
 * <p>Author: lwm</p>
 * <p>Date: 2025/5/28 22:57</p>
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/repairOrder")
public class RepairOrderController {

    private final RepairOrderService repairOrderService;

    /**
     * 分页查询报修单
     * @param pageReqDto
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).REPAIR_LIST.permission.identifier)")
    @PostMapping("/page")
    public R<Page<RepairOrderVO>> page(@RequestBody PageReqDto<RepairOrderPageReq> pageReqDto){
        Page<RepairOrderVO> page = repairOrderService.page(pageReqDto);
        return R.ok(page);
    }

    /**
     * 根据id查询报修单信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).REPAIR_INFO.permission.identifier)")
    @GetMapping("/info/{id}")
    public R<RepairOrderVO> info(@PathVariable("id") Integer id){
        return R.ok(repairOrderService.info(id));
    }

    /**
     * 保存报修单
     * @param repairOrderDTO
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).REPAIR_SAVE.permission.identifier)")
    @PostMapping
    public R<Boolean> save(@RequestBody RepairOrderDTO repairOrderDTO){
        return BR.result(repairOrderService.save(repairOrderDTO));
    }

    /**
     * 更新报修单
     * @param repairOrderDTO
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).REPAIR_UPDATE.permission.identifier)")
    @PutMapping
    public R<Boolean> update(@RequestBody @Validated RepairOrderDTO repairOrderDTO){
        return BR.result(repairOrderService.update(repairOrderDTO));
    }

    /**
     * 批量删除报修单
     * @param ids
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).REPAIR_DELETE.permission.identifier)")
    @DeleteMapping("/{ids}")
    public R<Boolean> remove(@PathVariable("ids") Integer[] ids){
        return BR.result(repairOrderService.deleteRepairOrderByIds(ids));
    }
}
